package BIO;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author:谢君臣
 * @Date:2021/7/23-9:37
 * @version:1.0
 * @function:
 */
public class Socket_test {
    public static void main(String[] args) {
        Server server = new Server();
        Client client = new Client();
        new Thread(server).start();
        new Thread(client).start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程结束");
    }

}
class Server implements Runnable{
    private static int port;
    private static InetAddress inetAddress;
    static{
        port = 9999;
        try {
            inetAddress =InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(port,0,inetAddress);
            Socket accept = serverSocket.accept();
            System.out.println(port);
            StringBuilder stringBuilder = new StringBuilder();
            byte[] data = new byte[1024];
            int length = 0;
            InputStream inputStream = accept.getInputStream();
            while ((length = inputStream.read(data))!=-1)
            {
                stringBuilder.append(new String(data,0,length));
            }
            System.out.println("server端==我收到的内容是：" +stringBuilder);
            OutputStream outputStream = accept.getOutputStream();
            outputStream.write("我是服务器端返回的数据".getBytes(StandardCharsets.UTF_8));
            accept.shutdownOutput();
            inputStream.close();
            outputStream.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
class Client implements Runnable{
    private static int port;
    private static InetAddress inetAddress;
    static{
        port = 9999;
        try {
            inetAddress =InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        try {
            Socket socket = new Socket(inetAddress, port);
            OutputStream outputStream = socket.getOutputStream();
            System.out.print("请输入通信内容：");
            Scanner in = new Scanner(System.in);
            String data_out = in.nextLine();
            in.close();
            outputStream.write(data_out.getBytes(StandardCharsets.UTF_8));
            socket.shutdownOutput(); // 结束输出
            StringBuilder stringBuilder = new StringBuilder();
            byte[] data = new byte[1024];
            int length = 0;
            InputStream inputStream = socket.getInputStream();
            while ((length = inputStream.read(data))!=-1)
            {
                stringBuilder.append(new String(data,0,length));
            }
            System.out.println("client端==我收到的内容是：" +stringBuilder);
            inputStream.close();
            outputStream.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}